package vn.vos.model;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
import java.util.Stack;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JSON {
	private JSONObject document;
	
	public JSON() {
		document = new JSONObject();				
	}
	
	public JSON(Object key, Object value) {
		document = new JSONObject();	
		document.put(key, value);
	}
	
	public JSONObject getDocument() {
		return document;
	}

	public void setDocument(JSONObject document) {
		this.document = document;
	}

	public void put(Object key, Object value) {
		document.put(key, value);
	}
	
	public Object get(Object key) {
		return document.get(key);
	}
	
	public String toString() {
		return document.toString();
	}
	
	 /**
     * @param src source string. It is not null
     * @param key key to find. 
     * @return the first value of key in dictionary
     */
    public String getDicData(String key) {
    	String src = this.toString();
		//check source
    	if(src == null)
    		return null;
    	
    	//get beginIndex
    	int index = src.indexOf(key);
    	if(index == -1)
    		return null;
    	else
    		index += key.length() + 2; // {"[string]":
    	
    	//get value data
    	StringBuilder sb = new StringBuilder();
    	if(src.charAt(index)!='"')
    		sb.append(src.charAt(index));
    	else
    		index++;    	
    	while( src.charAt(index)!='"' && src.charAt(index)!=',' && src.charAt(index)!='}')  {    		
    	    sb.append(src.charAt(index++));    		
    	}
    	
    	return sb.toString();
    }
    
    /**
     * @param src the dictionary which will be used. It is not null
     * @param key the key to search
     * @return a list which archives all values are appropriate with the key (at the same level)
     */
    public List<String> getAllDicData( String key ) {
    	Set set = this.document.entrySet();
    	String src = this.toString();
    	List<String> values = new ArrayList<String>();
    	
    	//check source
    	if(src == null)
    		return null;
    	
    	//get beginIndex
    	int index = src.indexOf(key);
    	if(index == -1)
    		return null;    
    	else	
    		index += key.length() + 2; // string":
    	    	    
    	
    	int len = src.length();  	
    	String s = "";
    	while(index < len) {    			
	    	if( src.charAt(index)!='"' && src.charAt(index)!=',' &&
	    			src.charAt(index)!='}' &&src.charAt(index)!='{'&& 
	    			src.charAt(index)!=':' && src.charAt(index)!=']'&& 
	    			src.charAt(index)!='[' )  {    		
	    	    s += src.charAt(index++);    		
	    	} else {
	    		if(!s.equals("")) {
	    			values.add(s);
	    			String sub = src.substring(index);
	    			index += sub.indexOf(key) + key.length();
	    		}	    			
	    		s="";
	    		index++;
	    	}	    	
    	}
    	
    	return values;
    }
    
    public JSON string2JSON(final String src) {
    	JSON json = new JSON();
    	
    	String key = "";
    	String value = "";
    	
    	/**get key and value*/
    	int len = src.length();
    	for(int i = 0; i < len; i++) {
    		if(src.charAt(i) == ':') {
    			key = src.substring(0, i-1);
    			value = src.substring(i+1,len);
    			break;
    		}
    	}    	
    	
    	/** need to optimize*/
    	key = key.replace('{',' ');
    	key = key.replace('}',' ');
    	key = key.replace('[',' ');
    	key = key.replace(']',' ');
    	key = key.replace('"',' ');
    	key = key.trim();    	
    	value = value.trim();    	
    	json.put(key, value);
    	return json;
    }
    
    
	public static void main(String args[]){		
		JSON json = new JSON();
		
		JSON object1=new JSON();
		object1.put("name","Amit Kumar1");
		object1.put("Max.Marks1",new Integer(100));
		object1.put("Min.Marks1",new Double(40));
		object1.put("Scored1",new Double(66.67));
		object1.put("nickname","Amit1");		
		
		JSON object2=new JSON();
		object2.put("name","Amit Kumar2");
		object2.put("Max.Marks2",new Integer(100));
		object2.put("Min.Marks2",new Double(40));
		object2.put("Scored2",new Double(66.67));
		object2.put("nickname","Amit2");		
		
		//1
		List<JSON> l = new ArrayList<JSON>();		 		
		l.add(object1);		
		l.add(object2);
		json.put("parent", l);			
		JSON jObj = json.string2JSON(json.toString());						
		
		System.out.println(json.toString());
		//System.out.print(jObj.toString());				
		
		List<String> values = json.getAllDicData("document");
		for(String s:values)
			System.out.println(s);
	}
}
